<template>
  <div class="image">
    <div class="image-add">
      <div class="-inner">
        <img v-if="image" :src="image" class="image" />
        <a @click.stop="selectResource" v-else
          ><i class="el-icon-plus avatar-uploader-icon"></i
        ></a>
      </div>
    </div>
    <div class="form-tip">
      <a class="image-md" v-if="image" @click.stop="selectResource">更换</a>
    </div>
    <resource-select
      :visible.sync="resourceDialog.visible"
      @update:selected="doSelectResource"
    ></resource-select>
  </div>
</template>

<script>
import ResourceSelect from "./resourceSelect";

export default {
  components: { ResourceSelect },
  name: "swak-image",
  data() {
    return {
      resourceDialog: {
        visible: false
      },
      image: ""
    };
  },
  props: {
    value: String
  },
  watch: {
    value(newValue) {
      this.image = newValue;
    },
    image(newValue) {
      this.$emit("input", newValue);
    }
  },
  methods: {
    selectResource: function() {
      this.resourceDialog.visible = true;
    },
    doSelectResource: function(resource) {
      this.image = resource.url;
    }
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
@require "../../common/stylus/variable.styl"
.image-add {
  .-inner {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    display inline-block;
    i {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .image {
      width: 178px;
      height: 178px;
      display block;
    }
    &:hover {
      border-color: #409EFF;
      i {
        color: #409EFF;
      }
    }
  }
}

.image-md {
  color $color-primary;
  cursor pointer;
}
</style>
